/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entity.FitnessProgram;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Administrator
 */
public class FitnessProgramDAO {
    private Connection cn;
    private CallableStatement cs;
    
    public FitnessProgramDAO() {
        cn = DB_Util.getConnection();
    }
    
    private ArrayList<FitnessProgram> getFitnessProgramData(ResultSet rs) throws SQLException {
        ArrayList<FitnessProgram> list = new ArrayList<FitnessProgram>();
        while (rs.next()) {
            FitnessProgram e = new FitnessProgram();
            e.setId(rs.getInt("FitnessProgramId"));
            e.setName(rs.getString("FitnessProgramName"));
            e.setLevel(rs.getInt("FitnessProgramLevel"));
            e.setPrice(rs.getInt("FitnessProgramPrice"));
            e.setDiscount(rs.getInt("FitnessProgramDiscount"));
            e.setDescription(rs.getString("FitnessProgramDescription"));
            list.add(e);
        }
        return list;
    }
    
    //DISPLAY FITNESSPROGRAMS:
    public ArrayList<FitnessProgram> getAllFitnessProgram() throws SQLException {
        cs = cn.prepareCall("{call SP_Display_FitnessProgram}");
        ResultSet rs = cs.executeQuery();
        return getFitnessProgramData(rs);
    }
    
    public void addFitnessProgram(FitnessProgram item) throws SQLException
    {
        cs = cn.prepareCall("{call SP_Add_FitnessProgram(?,?,?,?,?)}");
        cs.setNString(1, item.getName());
        cs.setInt(2, item.getLevel());
        cs.setNString(3, item.getDescription());
        cs.setInt(4, item.getPrice());
        cs.setFloat(5, item.getDiscount());
        cs.execute();
    }
    public void editFitnessProgram(FitnessProgram item) throws SQLException{
        cs = cn.prepareCall("{call SP_Edit_FitnessProgram(?,?,?,?,?,?)}");
        cs.setInt(1,item.getId());
        cs.setNString(2, item.getName());
        cs.setInt(3, item.getLevel());
        cs.setNString(4, item.getDescription());
        cs.setInt(5, item.getPrice());
        cs.setFloat(6, item.getDiscount());
        cs.execute();
    }
      public void deleteFitnessProgram(int id) throws SQLException{
        cs = cn.prepareCall("{call SP_Delete_FitnessProgram(?)}");
        cs.setInt(1,id);
        cs.execute();
    }
      
    
}
